﻿using CPPEI.Coolzon.Designtime.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public class AreasInSceneResizeOperation : OperationBase
    {
        private DesignView _designView;
        /// <summary>
        /// 组件, oldLeft, oldWidth, newLeft, newWidth
        /// </summary>
        private List<Tuple<Coolzon.UI.Part, double, double, double, double>> _areas;
        public AreasInSceneResizeOperation(DesignView designView, Coolzon.UI.ScenePart scene, Coolzon.UI.AreaPart resizeArea, double left, double width)
        {
            _designView = designView;
            _areas = new List<Tuple<Coolzon.UI.Part, double, double, double, double>>();
            double leftOffset, widthOffset;
            leftOffset = left - resizeArea.XLeft;
            widthOffset = width - resizeArea.XWidth;

            //只改变宽度,则重设该Area右侧Area的Left
            if (Math.Abs(widthOffset) > 0.001 && Math.Abs(leftOffset) < 0.001)
            {
                string curAreaName = resizeArea.Name;
                Coolzon.UI.AreaPart rightArea = scene.GetRightRootArea(resizeArea);
                //右侧存在其他区域
                while (!string.Equals(rightArea.Name, curAreaName))
                {
                    Tuple<Coolzon.UI.Part, double, double, double, double> tmpArea = new Tuple<Coolzon.UI.Part, double, double, double, double>(rightArea, rightArea.XLeft, rightArea.XWidth, rightArea.XLeft + widthOffset, rightArea.XWidth);
                    _areas.Add(tmpArea);

                    curAreaName = rightArea.Name;
                    rightArea = scene.GetRightRootArea(rightArea);
                }
                Tuple<Coolzon.UI.Part, double, double, double, double> curArea = new Tuple<Coolzon.UI.Part, double, double, double, double>(resizeArea, resizeArea.XLeft, resizeArea.XWidth, left, width);
                _areas.Add(curArea);
            }
            else
            {
                int leftAreaCount = 0;
                string curAreaName = resizeArea.Name;
                List<Coolzon.UI.AreaPart> leftAreas = new List<Coolzon.UI.AreaPart>();
                Coolzon.UI.AreaPart leftArea = scene.GetLeftRootArea(resizeArea);
                //左侧存在其他区域
                while (!string.Equals(leftArea.Name, curAreaName))
                {
                    leftAreaCount++;
                    leftAreas.Add(leftArea);

                    curAreaName = leftArea.Name;
                    leftArea = scene.GetLeftRootArea(leftArea);
                }
                for (int i = 0; i < leftAreaCount; i++)
                {
                    Coolzon.UI.AreaPart area = leftAreas[i];
                    double newLeft = area.XLeft + (leftAreaCount - i - 1) * leftOffset / leftAreaCount;
                    double newWidth = area.XWidth + leftOffset / leftAreaCount;
                    Tuple<Coolzon.UI.Part, double, double, double, double> tmpArea = new Tuple<Coolzon.UI.Part, double, double, double, double>(area, area.XLeft, area.XWidth, newLeft, newWidth);
                    _areas.Add(tmpArea);
                }
                if(_areas.Count == 0)
                {
                    Tuple<Coolzon.UI.Part, double, double, double, double> curArea = new Tuple<Coolzon.UI.Part, double, double, double, double>(resizeArea, resizeArea.XLeft, resizeArea.XWidth, resizeArea.XLeft, resizeArea.XWidth);
                    _areas.Add(curArea);
                }
                else
                {
                    Tuple<Coolzon.UI.Part, double, double, double, double> curArea = new Tuple<Coolzon.UI.Part, double, double, double, double>(resizeArea, resizeArea.XLeft, resizeArea.XWidth, left, width);
                    _areas.Add(curArea);
                }
            }
        }
        protected override void OnDo()
        {
            foreach (var area in _areas)
            {
                try
                {
                    area.Item1.XLeft = area.Item4;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(area.Item1.Metadata, "Left", typeof(double), area.Item4);
                    area.Item1.XWidth = area.Item5;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(area.Item1.Metadata, "Width", typeof(double), area.Item5);
                }
                catch (Exception ex)
                {
                    Common.Logger.ErrorToTag(Library.LogTAG, "更新场景内区域{0}大小宽高属性出错,ex:{1}", area.Item1.Name, ex);
                }
            }
        }

        protected override void OnUndo()
        {
            foreach (var area in _areas)
            {
                try
                {
                    area.Item1.XLeft = area.Item2;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(area.Item1.Metadata, "Left", typeof(double), area.Item2);
                    area.Item1.XWidth = area.Item3;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(area.Item1.Metadata, "Width", typeof(double), area.Item3);
                }
                catch (Exception ex)
                {
                    Common.Logger.ErrorToTag(Library.LogTAG, "更新场景内区域{0}大小宽高属性出错,ex:{1}", area.Item1.Name, ex);
                }
            }
        }
    }
}
